package com.sam.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sam.chat.entity.ChatUserDao;
import com.sam.chat.mapper.ChatUserMapper;
import com.sam.chat.service.IChatUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sam.chat.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sam
 * @since 2022-12-31
 */
@Service
public class ChatUserServiceImpl extends ServiceImpl<ChatUserMapper, ChatUserDao> implements IChatUserService {

    @Autowired
    private ChatUserMapper chatUserMapper;

    @Override
    @Transactional
    public UserVo loginByPassword(String username, String password) {
        UserVo userVo = chatUserMapper.findUser(username, password);
        return userVo;

    }

    @Override
    public boolean checkUsername(String username) {
        QueryWrapper<ChatUserDao> query1 = new QueryWrapper<>();
        query1.eq("username", username);
        int nicknameCount = chatUserMapper.selectCount(query1);
        return nicknameCount == 0;
    }

    @Override
    public boolean checkPhone(String phone) {
        QueryWrapper<ChatUserDao> query2 = new QueryWrapper<>();
        query2.eq("phone", phone);
        int phoneCount = chatUserMapper.selectCount(query2);
        return phoneCount == 0;
    }


    @Override
    public boolean register(String username, String nickname, String password, String phone, String header, int sex,int age) {
        ChatUserDao chatUserDao = new ChatUserDao();
        chatUserDao.setUsername(username);
        chatUserDao.setNickname(nickname);
        chatUserDao.setPassword(password);
        chatUserDao.setPhone(phone);
        chatUserDao.setHeader(header);
        chatUserDao.setSex(sex);
        chatUserDao.setRole(1);
        chatUserDao.setAge(age);
        int result = chatUserMapper.insert(chatUserDao);
        return result == 1;
    }


}
